package com.shenmazong.utils;


import com.shenmazong.vo.TokenInfo;
import io.jsonwebtoken.*;

import java.util.Date;

/**
 * @author Qi
 * @version 1.0
 * @description TODO
 * @date 2021/7/16 15:08
 */

public class JwtUtils {
    public static String makeToken(Long id, String name, String role) {
        //--1
        JwtBuilder builder = Jwts.builder();

        builder.setId(String.valueOf(id));
        builder.setSubject(name);

        // jwt 生成时间
        builder.setIssuedAt(new Date());

        // jwt 过期时间
        long now = System.currentTimeMillis();
        long exp = now+1000*60*60*24;
        builder.setExpiration( new Date( exp ) );

        // 数据区
        builder.claim( "loginId", id );
        builder.claim( "loginName",name );
        builder.claim( "loginRole", role );

        // 设置jwt签名算法
        builder.signWith(SignatureAlgorithm.HS256,"123456");  //了解256 算法特性  以及其他算法

        // 生产token

        return  builder.compact();

    }
    public static boolean verifyToken(String token) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey("123456")
                    .parseClaimsJws(token)
                    .getBody();

            Long loginId = Long.valueOf(claims.get("loginId").toString());
            String loginName = (String)claims.get("loginName");
            String loginRole = (String)claims.get("loginRole");
            String loginRealName = (String)claims.get("loginRealName");
            System.out.println(loginId+","+loginName+","+loginRole+","+loginRealName);
            return true;
        }
        catch (ExpiredJwtException e) {
            System.out.println("token expired");
        } catch (SignatureException e) {
            System.out.println("token signature error");
        } catch (Exception e) {
            System.out.println("token error");
        }

        return false;
    }

    public static String makeTokenByInfo(TokenInfo info) {
        return makeToken(info.getLoginId(), info.getLoginName(), info.getLoginRole());
    }

    public static TokenInfo getTokenByString(String token) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey("123456")
                    .parseClaimsJws(token)
                    .getBody();
            TokenInfo info = new TokenInfo();
            info.setLoginId(Long.valueOf(claims.get("loginId").toString()));
            info.setLoginName((String)claims.get("loginName"));
            info.setLoginRole((String)claims.get("loginRole"));
            info.setLoginRealName((String)claims.get("loginRealName"));
            return info;
        }
        catch (ExpiredJwtException e) {
            Claims claims = e.getClaims();
            TokenInfo info = new TokenInfo();
            info.setLoginId((Long)claims.get("loginId"));
            info.setLoginName((String)claims.get("loginName"));
            info.setLoginRole((String)claims.get("loginRole"));
            info.setLoginRealName((String)claims.get("loginRealName"));
            return info;
        } catch (SignatureException e) {
            System.out.println("token signature error");
        } catch (Exception e) {
            System.out.println("token error");
        }
        return null;
    }

    public static String makeToken(Long id, String name, String role,String realName) {
        //--1
        JwtBuilder builder = Jwts.builder();

        builder.setId(String.valueOf(id));
        builder.setSubject(name);

        // jwt 生成时间
        builder.setIssuedAt(new Date());

        // jwt 过期时间
        long now = System.currentTimeMillis();
        long exp = now+1000*60*60*24;
        builder.setExpiration( new Date( exp ) );

        // 数据区
        builder.claim( "loginId", id );
        builder.claim( "loginName",name );
        builder.claim( "loginRole", role );
        builder.claim( "loginRealName", realName );

        // 设置jwt签名算法
        builder.signWith(SignatureAlgorithm.HS256,"123456");  //了解256 算法特性  以及其他算法

        // 生产token

        String token =  builder.compact();
        System.out.println(token);
        return token;
    }


}
